﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using _180FrameWork.CustControls.Models;
//using _180FrameWork.Common.Models;

namespace _180FrameWork.CustControls
{
    public partial class frmDataGridViewQuery : Form
    {
        private CustDataGridView _dg { get; set; }
        public CustQueryCondition QC
        {
            get;
            private set;
        }

        public frmDataGridViewQuery(CustDataGridView dg)
        {
            _dg = dg;
            InitializeComponent();
            InitCbConnection();          
            InitCbColumn(dg);
            InitTxtInfo(dg);
        }

        private void InitCbConnection()
        {
            IList<KeyValueDict> dict = new List<KeyValueDict>();
            dict.Add(new KeyValueDict() { DM = "and", MC = "并且" });
            dict.Add(new KeyValueDict() { DM = "or", MC = "或者" });
            this.cbConnection.DataSource = dict;
        }

        private void InitCbCondition(string ColumnType)
        {
            IList<KeyValueDict> dict = new List<KeyValueDict>();
            dict.Add(new KeyValueDict() { DM = "=", MC = "等于" });
            dict.Add(new KeyValueDict() { DM = "<>", MC = "不等于" });

            switch (ColumnType)
            {
                case "STRING":
                    dict.Add(new KeyValueDict() { DM = "like", MC = "类似" });
                    break;
                default:
                    dict.Add(new KeyValueDict() { DM = ">", MC = "大于" });
                    dict.Add(new KeyValueDict() { DM = ">=", MC = "大于等于" });
                    dict.Add(new KeyValueDict() { DM = "<", MC = "小于" });
                    dict.Add(new KeyValueDict() { DM = "<=", MC = "小于等于" });
                    break;
            }
            this.cbCondition.DataSource = dict;
        }

        private void InitCbColumn(CustDataGridView dg)
        {
            IList<KeyValueDict> dict = new List<KeyValueDict>();
            int selectindex = 0;
            foreach (DataGridViewColumn column in dg.Columns)
            {
                if (dg.CurrentCell != null && dg.CurrentCell.ColumnIndex == column.Index)
                {
                    selectindex = column.Index;
                }
                dict.Add(new KeyValueDict() { DM = column.DataPropertyName, MC = column.HeaderText });
            }
            this.cbColumn.DataSource = dict;
            this.cbColumn.SelectedIndex = selectindex;
        }

        private void InitTxtInfo(CustDataGridView dg)
        {
            if (dg.CurrentCell != null)
            {
                this.txtInfo.Text = dg.CurrentCell.Value.ToString();
            }
        }

        private void frmDataGridViewFilter_Load(object sender, EventArgs e)
        {

        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            //数据校验            
            string strinfo = this.txtInfo.GetValue().ToString();
            object infoValue = null;
            bool IsValueFit = true;
            try
            {
                var column = this._dg.Columns[cbColumn.SelectedIndex];
                switch (column.ValueType.Name.ToUpper())
                {
                    case "DATETIME":
                        infoValue = Convert.ToDateTime(strinfo);
                        break;
                    case "INT":
                        infoValue = Convert.ToInt32(strinfo);
                        break;
                    case "DECIMAL":
                    case "LONG":
                    case "FLOAT":
                        infoValue = Convert.ToDecimal(strinfo);
                        break;
                    default:
                        infoValue = Convert.ToString(strinfo);
                        break;
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
                IsValueFit = false;
            }

            if (IsValueFit)
            {              
                QC = new CustQueryCondition();
                QC.DisPlay = string.Format("{0} {1} {2} {3}", new object[] {
                    this.cbConnection.Text,
                    this.cbColumn.Text,
                    this.cbCondition.Text,
                    this.txtInfo.Text
                });

                if (this.cbCondition.GetValue().ToString() == "like")
                {
                    QC.Condition = string.Format(" {0} {1} {2} '%' || :{1} || '%' ", new object[] {
                    this.cbConnection.GetValue(),
                    this.cbColumn.GetValue(),
                    this.cbCondition.GetValue()});
                }
                else
                {
                    QC.Condition = string.Format(" {0} {1} {2} :{{0}} ", new object[] {
                    this.cbConnection.GetValue(),
                    this.cbColumn.GetValue(),
                    this.cbCondition.GetValue()});
                }

                QC.Key = "Param_"+ Guid.NewGuid().ToString();

                QC.Value = infoValue;

                this.DialogResult = DialogResult.OK;
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }

        private void cbColumn_SelectedIndexChanged(object sender, EventArgs e)
        {
            this.txtInfo.Text = "";
            var column = this._dg.Columns[cbColumn.SelectedIndex];
            string columnType = column.ValueType.Name.ToString().ToUpper();
            InitCbCondition(columnType);
        }
    }
}
